/*
 * INSANE - Interactive Structural Analysis Environment
 *
 * Copyright (C) 2003-2006
 * Universidade Federal de Minas Gerais
 * Escola de Engenharia
 * Departamento de Engenharia de Estruturas
 *
 * Author's email :     insane@dees.ufmg.br
 * Author's Website :   http://www.dees.ufmg.br/insane
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */
 
package br.ufmg.dees.insane.draw;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.QuadCurve2D;
import java.util.Vector;

/**
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @since	April 2004
 */
public class QuadCurveDraw extends Draw {
	
//*****************************************************************************
	
	public QuadCurveDraw(int x1, int y1, int cx, int cy, int x2, int y2 ) {
		this(new Point(x1, y1), new Point(cx, cy), new Point(x2, y2));
	}
	
//*****************************************************************************
	
	public QuadCurveDraw(Point p1, Point c, Point p2) {
		this(p1, c, p2, null);
	}
	
//*****************************************************************************
	
	public QuadCurveDraw(Point p1, Point c, Point p2, Object represents) {
		this.setLocation(p1);
		this.setSize(new Dimension(p2.x, p2.y));
		this.setControl(c);
		this.setRepresents(represents);
	}
	
//*****************************************************************************
	
	public boolean equals(Object obj) {
		if (super.equals(obj) && obj instanceof QuadCurveDraw) {
			QuadCurveDraw d = (QuadCurveDraw)obj;
			if (d.getP2().equals(getP2())){
				if (d.getControl().equals(getControl())){
					return true;
				}
			}
		}
		return false;
	}
	
//*****************************************************************************
	
	public Point getP1() {
		return this.getLocation();
	}
	
//*****************************************************************************
	
	public Point getP2() {
		Point po = new Point(this.getSize().width, this.getSize().height);
		return po;
	}
	
//*****************************************************************************
	
	public double getLength() {
		double dx = this.getX2() - this.getX1();
		double dy = this.getY2() - this.getY1();
		double l = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
		return l;
	}
	
//*****************************************************************************
	
	public double getX1() {
		return getLocation().x;
	}
	
//*****************************************************************************
	
	public double getY1() {
		return getLocation().y;
	}
	
//*****************************************************************************
	
	public double getX2() {
//		return getSize().width;
		return getP2().x;
	}
	
//*****************************************************************************
	
	public double getY2() {
//		return getSize().height;
		return getP2().y;
	}
	
//*****************************************************************************
	
	public double getXCtrl() {
		return getControl().x;
	}
	
//*****************************************************************************
	
	public double getYCtrl() {
		return getControl().y;
	}
	
//*****************************************************************************
	
	public Vector<Object> getShapes() {
		return null;
	}
	
//*****************************************************************************
	
	public double getAngle() {
		double theta;
		double dx = getX2() - getXCtrl();
		double dy = getY2() - getYCtrl();
		theta = Math.atan2(-dy, dx);
		return theta;
	}
	
//*****************************************************************************
	
	/**
	 * Draws in a given Graphics.
	 *
	 * @see   java.awt.Graphics
	 */
	public void draw(Graphics2D g2) {
		
		int x1 = (int)getX1();
		int y1 = (int)getY1();
		int cx = (int)getXCtrl();
		int cy = (int)getYCtrl();
		int x2 = (int)getX2();
		int y2 = (int)getY2();
		
		g2.draw(new QuadCurve2D.Double(x1, y1, cx, cy, x2, y2));
		if (!this.getLabel().equals("")) {
			double ang = this.getAngle();
			double ang2 = Math.atan2(15, (this.getLength()/2));
			double l = 15/Math.sin(ang2);
			double x = x1 + l*Math.cos(ang+ang2);
			double y = y1 + l*Math.sin(ang+ang2);
			g2.drawString(this.getLabel(), (int)x, (int)y);
		}
		
		Color currentColor = g2.getColor();
		
		if (isSelected()) {
			g2.setColor(new Color(255, 0, 0));
			g2.draw(new QuadCurve2D.Double(x1, y1, cx, cy, x2, y2));
		}
		
		g2.setColor(currentColor);
	}
	
//*****************************************************************************
	
	/**
	 * Returns the draw bounds in device units.
	 *
	 * @return the draw bounds
	 */
	public Rectangle getBounds() {
		
		int x1 = (int)getX1();
		int y1 = (int)getY1();
		int cx = (int)getXCtrl();
		int cy = (int)getYCtrl();
		int x2 = (int)getX2();
		int y2 = (int)getY2();
		
//		Rectangle rect = new Rectangle(x1, y1, x2 - x1, y2 - y1);
//		return rect;
		
		QuadCurve2D.Double qc = new QuadCurve2D.Double(x1, y1, cx, cy, x2, y2);
		return qc.getBounds();
	}
	
//*****************************************************************************
	
	public boolean contains(Point p) {
		
		int x1 = (int)getX1();
		int y1 = (int)getY1();
		int cx = (int)getXCtrl();
		int cy = (int)getYCtrl();
		int x2 = (int)getX2();
		int y2 = (int)getY2();
		
		QuadCurve2D.Double quadc = new QuadCurve2D.Double(x1, y1, cx, cy, x2, y2);
		return quadc.intersects(p.x - 3, p.y -3, 6, 6);
	}
	
//*****************************************************************************
}
